package com.gaoboy.springbootgoods.service.impl;

import com.gaoboy.springbootgoods.entity.User;
import com.gaoboy.springbootgoods.mapper.UserMapper;
import com.gaoboy.springbootgoods.service.UserService;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> queryAll() {
        return userMapper.queryAll();
    }

    @Override
    public User doLogin(String username, String password) {
        User user = userMapper.findByNameAndPassword(username, password);
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        return user;
    }

    @Override
    public String register(String username, String password) {
        User old = userMapper.findByName(username);
        if (old != null) {
            return "exist";
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setCreateTime(new Date());
        user.setValidateEndTime(new Date());
        int i = userMapper.insert(user);
        return i > 0 ? "success" : "fail";
    }
}
